package com.rbs.Controller;

import com.rbs.entity.Phone;
import com.rbs.service.Impl.PhoneServiceImpl;
import com.rbs.service.PhoneService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(urlPatterns = "/SelectByPageServlet")
public class SelectByPageServlet extends HttpServlet {
    private PhoneService phoneService = new PhoneServiceImpl();
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //想办法获取三个变量的值放入到request作用域
        //第一 5条数据 第二 当前页号 第三 总页数
        //尝试从请求路径中获取页号，第一次获取不到，通过点击上一页或者下一页的时候获取到
        String curPageStr = request.getParameter("curPage");
        int curPage = 1;//给当前页数 默认值为1 当curPageStr不为null时 重新赋值
        if (curPageStr != null){
            curPage = Integer.parseInt(curPageStr);
        }
        //获取当前页的数据
        int pageSize = 3;//一页展示几条是固定的
        int start = (curPage-1)*pageSize;
        List<Phone> phoneList = phoneService.selectByPage(start, pageSize);
            request.setAttribute("phoneList",phoneList);
            request.setAttribute("curPage",curPage);//把当前页号存入作用域
        //总条数
        int count = phoneService.selectCount();
        //获取总页数
        int totalPage = count%pageSize==0 ?count/pageSize :count/pageSize+1;
        request.setAttribute("totalPage",totalPage);
        request.getRequestDispatcher("/showAllPhone.jsp").forward(request,response);
    }
}
